package com.orgcent.activities.widget;  
import java.util.Timer;
import java.util.TimerTask;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.util.Log;
import android.widget.RemoteViews;

import com.orgcent.activities.R;

/**
 * 桌面小部件，app widget
 */
public class AppWidgetDemo extends AppWidgetProvider {  
    
	private static final String TAG = AppWidgetDemo.class.getSimpleName();
    int count;
    
    @Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
		Log.i(TAG, "onEnabled```");
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);
		Log.i(TAG, "onDeleted```");
	}


	@Override
	public void onDisabled(Context context) {
		super.onDisabled(context);
		Log.i(TAG, "onDisabled```");
	}


	@Override  
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
            int[] appWidgetIds) {  
	
		Log.i(TAG, "onUpdate```");
		
        Timer timer = new Timer();  
        timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), 1, 6000);  
        super.onUpdate(context, appWidgetManager, appWidgetIds);  
    }  
      
      
    private class MyTime extends TimerTask{  
        RemoteViews remoteViews;  
        AppWidgetManager appWidgetManager;  
        ComponentName thisWidget;  
          
        public MyTime(Context context,AppWidgetManager appWidgetManager){  
            this.appWidgetManager = appWidgetManager;  
            remoteViews = new RemoteViews(context.getPackageName(),R.layout.app_widget_main);  
              
            thisWidget = new ComponentName(context,AppWidgetDemo.class);  
        }  
        public void run() {  
        	
            remoteViews.setTextViewText(R.id.wordcup, "刷新" + ++count +"天");  
            appWidgetManager.updateAppWidget(thisWidget, remoteViews);  
        }  
          
    }  
    
}  